#include <bits/stdc++.h>
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Rev(i,a,b) for(int i=a;i>=b;i--)
#define Fin(file) freopen(file,"r",stdin)
#define Fout(file) freopen(file,"w",stdout)
using namespace std; using ll = long long;
constexpr int N=1e3+5,mod=1e9+7;
class Int{
    int x;
public:
    explicit Int(int _x=0) : x(_x) {}
    Int& operator+= (const Int& O) { unsigned t=x+O.x; x=min(t,t-mod); return *this; }
    Int& operator-= (const Int& O) { unsigned t=x-O.x; x=min(t,t+mod); return *this; }
    Int& operator*= (const Int& O) { x=1ll*x*O.x%mod; return *this; }
    friend Int operator+ (Int A,const Int& B) { return A+=B; }
    friend Int operator- (Int A,const Int& B) { return A-=B; }
    friend Int operator* (Int A,const Int& B) { return A*=B; }
    int operator() () const { return x; }
};
int n,a[N],b[N]; Int cp[N*2],ci[N*2],inv[N*2],Cat[N],f[N][N];
int main(){
    Fin("zha.in"); Fout("zha.out");
    cin>>n; For(i,1,n) cin>>a[i];; For(i,1,n) cin>>b[i];
    inv[1]=Int(1); For(i,2,n*2) inv[i]=Int(mod-mod/i)*inv[mod%i];
    cp[0]=Int(1); For(i,1,n*2) cp[i]=cp[i-1]*Int(i);
    ci[0]=Int(1); For(i,1,n*2) ci[i]=ci[i-1]*inv[i];
    For(i,0,n) Cat[i]=cp[2*i]*ci[i]*ci[i]*inv[i+1];
    Rev(i,n,1) Rev(j,n,1){
        if(i==n&&j==n) { f[i][j]=Int(1); continue; }
        if(i<n) f[i][j]+=f[i+1][j];
        if(j<n) f[i][j]+=f[i][j+1];
        if(i<n&&j<n&&a[i]==b[j]){
            int tt=0;
            for(int i0=i+1,j0=j+1;i0<=n&&j0<=n;i0++,j0++){
                if(a[i0]==b[j0]){
                    f[i][j]-=f[i0][j0]*Cat[tt]; tt++;
                }
            }
        }
    }
    cout<<f[1][1]()<<'\n';
    return 0;
}